//package cn.microservices.shpplychain.gateway.filter;
//
//import cn.microservices.shpplychain.gateway.base.enums.TokenKey;
//import cn.microservices.shpplychain.gateway.interpector.ip.IpInterceptor;
//import cn.microservices.shpplychain.gateway.interpector.rquest.OnceRequestInterceptor;
//import cn.microservices.shpplychain.gateway.util.RedisHelperUtil;
//import cn.microservices.shpplychain.gateway.util.WebFluxUtil;
//import cn.hutool.crypto.Mode;
//import cn.hutool.crypto.Padding;
//import cn.hutool.crypto.symmetric.AES;
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.Ordered;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebFilter;
//import org.springframework.web.server.WebFilterChain;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.Resource;
//import java.nio.charset.StandardCharsets;
//import java.util.Base64;
//
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
//
//
//@Configuration
//public class SecurityFilter implements WebFilter, Ordered {
////    private String pass = "qazxswedcfrtgbnm";
////    private String pre = "/microservices";
//    private Logger log = LoggerFactory.getLogger(getClass());
//
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @Autowired
//    private IpInterceptor ipInterceptor;
//
//    @Autowired
//    private OnceRequestInterceptor onceRequestInterceptor;
////    @Value("${spring.profiles.active}")
////    private String active;
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
//
//        ServerHttpRequest request = exchange.getRequest().mutate()
//                .header("from", "gateway").build();
//        ServerHttpResponse response = exchange.getResponse();
//        String rawPath = request.getURI().getRawPath();
//
//        String timestamp = request.getHeaders().getFirst("timestamp");
//        HttpHeaders headers = request.getHeaders();
//        log.info(JSON.toJSON(headers.entrySet()).toString());
//
//        Long t = System.currentTimeMillis();
////加解密
////        if(isProd()){
////            if (StringUtils.isEmpty(timestamp)){
////                log.warn("请求时间戳为空 path={}",rawPath);
////                return WebFluxUtil.err("请求时间戳为空");
////            }
////            if (Math.abs(Long.valueOf(timestamp)- t) >3000){
////                log.warn("用户请求时间过长");
////                throw new RuntimeException("请求超时");
////            }
////            String time = timestamp+"000";
////            String url = rawPath.substring(pre.length(),rawPath.length()-32);
////            //3，将uri解析成正确的url
////            rawPath =pre+decryptAES(url,pass,time);
////            log.info("解密后 url={},rawPath={},timestamp={}",url,rawPath,timestamp);
////        }
//
////        String token = request.getHeaders().getFirst("token");
////        if (StringUtils.isEmpty(token)){
////            log.warn("请求Token为空");
////            return WebFluxUtil.err("请求Token为空");
////        }
////        Object userIdObj = redisTemplate.opsForValue().get("");
////        if (userIdObj==null){
////            log.warn("token失效");
////            return WebFluxUtil.err("token失效");
////        }
////        String userId = userIdObj.toString();
////        request = exchange.getRequest().mutate()
////                .header("userId", userId).build();
//        request = exchange.getRequest().mutate()
//                .header("userId", "userId").build();
//        ServerHttpRequest newRequest = request.mutate().path(rawPath).build();
//        log.info("请求拦截 通过 newRequest={}",JSON.toJSONString(newRequest));
//
//        //ip拦截
//        if (ipInterceptor.isIllegalByIp(newRequest)) {
//            log.warn("IP拦截请求失败");
//            return WebFluxUtil.err("IP拦截请求失败");
//        }
//        //一次请求
////        if (onceRequestInterceptor.isInvalid(newRequest)) {
////            log.warn("一次请求失败");
////            return WebFluxUtil.err("一次请求失败");
////        }
//
//        ServerWebExchange newExchange = exchange.mutate().request(newRequest).response(response).build();
//        newExchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
//
//        return chain.filter(newExchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return -9999;
//    }
//
//    /**
//     * AES 解密
//     * @param data
//     * @param pass
//     * @return
//     */
//    private static String decryptAES(String data, String pass,String timestamp) {
//        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding,
//              pass.getBytes(),
//                timestamp.getBytes());
//        byte[] buf = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8));
//        String result = aes.decryptStr(buf);
//        return result;
//    }
//
////    private boolean isProd(){
////        if (1==1){
////            return true;
////        }
////        log.info("是否正式线 active={}",active);
////        if(!StringUtils.isEmpty(active)&&active.equals("prod")){
////            return true;
////        }
////        return false;
////    }
//
//}
